
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: '../../service/admin/getAllLevel',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapLevelToView(responseContent);
        }
    });

}

function bindEvent() {
    ISS.EVENT.bindEvent('#AdMemberLevel_Add', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Đăng kí cấp bậc mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#AdMemberLevelCommon_Modified', 'click', function () {
        toEditPage();
    });
}

function sendInfo() {
    var level = {};

    level.maCB = ISS.DOM.getVal('#AdMemberLevel_code');
    level.tenCB = ISS.DOM.getVal('#AdMemberLevel_name');

    ISS.BACKEND.request({
        url: '../../service/admin/header/adMemberLevel',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(level),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}

function toEditPage() {
    var _maCB = []
    ISS.DOM.forEach('#form-AdMemberLevelCommon input[type="checkbox"]:checked', function(){
        _maCB.push($(this).val());
    });

    if(_maCB.length == 1) {
        var _url = '/service/admin/toLevelMemberPage/' + _maCB[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Không được chọn hơn một dòng')
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#AdMemberLevel_code', 'error');
    ISS.DOM.removeClass('#AdMemberLevel_name', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapLevelToView(level) {

    var rows = level.length;
    var columns = 1;
    var str = '';
    str += '<table id="LevelTable" class="tableLevelWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnLevelSTT">STT</th>';
    str += '       <th class="columnLevelStick">Tick</th>';
    str += '       <th class="columnLevelCode">Mã CB</th>';
    str += '       <th class="columnLevelName">Tên cấp bậc</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }else{
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnLevelSTT tableLevelPadding">' + i + '</td>';
            str += '<td class="columnLevelStick tableLevelPadding"><input onclick="highlight_row(this)" id="' + level[i].maCB + '" type="checkbox" value="' + level[i].maCB + '"/></td>';
            str += '<td class="columnLevelCode tableLevelPadding">' + level[i].maCB + '</td>';
            str += '<td class="columnLevelName tableLevelPadding">' + level[i].tenCB + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#AdMemberLevelCommon_All', str);
}